package com.onix.actions;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

public class PageInformation extends Activity {
	private EtwasAdapter mHelper;
	private TextView contentTitle;
	private ImageView menuImage;
	private WebView textWebView;
	private Bundle extras;
	private int lastOrienration = 666;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_information);

		mHelper = new EtwasAdapter(this);

		changeBackgroundOrientation(
				getResources().getConfiguration().orientation,
				R.drawable.bg_second_v, R.drawable.bg_second_h);

		makePageFace();

	}

	public void changeBackgroundOrientation(int outerOrientation,
			int inBgPortret, int inBgLandscape) {
		if (lastOrienration != outerOrientation) {
			if (outerOrientation == Configuration.ORIENTATION_LANDSCAPE) {
				this.getWindow().setBackgroundDrawableResource(inBgLandscape);
			} else {
				this.getWindow().setBackgroundDrawableResource(inBgPortret);
			}
			lastOrienration = outerOrientation;
		}
	}

	public void makePageFace() {
		contentTitle = (TextView) findViewById(R.id.content_title);
		textWebView = (WebView) findViewById(R.id.content_text);

		extras = getIntent().getExtras();
		if (extras != null) {
			switch (extras.getInt("page")) {
			case R.id.menu_about:
				menuImage = (ImageView) findViewById(R.id.img_about_bg);
				mHelper.makePage(menuImage, R.drawable.icon_about_is,
						contentTitle, R.string.menu_item_about, textWebView,
						"content_about.html");
				break;

			case R.id.menu_careers:
				menuImage = (ImageView) findViewById(R.id.img_careers_bg);
				mHelper.makePage(menuImage, R.drawable.icon_careers_is,
						contentTitle, R.string.menu_item_careers, textWebView,
						"content_careers.html");
				break;

			case R.id.menu_clients:
				menuImage = (ImageView) findViewById(R.id.img_clients_bg);
				mHelper.makePage(menuImage, R.drawable.icon_clients_is,
						contentTitle, R.string.menu_item_clients, textWebView,
						"content_clients.html");
				break;

			case R.id.menu_contacts:
				menuImage = (ImageView) findViewById(R.id.img_contacts_bg);
				mHelper.makePage(menuImage, R.drawable.icon_contacts_is,
						contentTitle, R.string.menu_item_contacts, textWebView,
						"content_contacts.html");
				break;

			case R.id.menu_services:
				menuImage = (ImageView) findViewById(R.id.img_services_bg);
				mHelper.makePage(menuImage, R.drawable.icon_services_is,
						contentTitle, R.string.menu_item_services, textWebView,
						"content_services.html");
				break;
			}
		}
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		changeBackgroundOrientation(newConfig.orientation,
				R.drawable.bg_second_v, R.drawable.bg_second_h);
		super.onConfigurationChanged(newConfig);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_base, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return mHelper.selectMenu(item.getItemId());
	}
}
